package com.xxwy.bean.register;

import com.xxwy.bean.User;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.support.GenericApplicationContext;

/**
 * @author wy
 * @date 2020/4/21 13:45
 */
public class BeanDefinitionRegistryDemo {

  public static void main(String[] args) {
    GenericApplicationContext applicationContext = new GenericApplicationContext();

    register(applicationContext,"x");

    applicationContext.refresh();
    User user = applicationContext.getBean("x",User.class);
    System.out.println(user);

  }

  public static void register(BeanDefinitionRegistry registry, String beanName ){
    BeanDefinitionBuilder definitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(User.class);
    definitionBuilder.addPropertyValue("age","11")
        .addPropertyValue("name","register");

    registry.registerBeanDefinition(beanName,definitionBuilder.getBeanDefinition());
  }
}
